package com.renchunlin.taobaounion.ui.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.renchunlin.taobaounion.R;
import com.renchunlin.taobaounion.base.BaseActivity;
import com.renchunlin.taobaounion.custom.LoadingView;
import com.renchunlin.taobaounion.model.domain.TicketResult;
import com.renchunlin.taobaounion.presenter.ITicketPresenter;
import com.renchunlin.taobaounion.utils.PresenterManager;
import com.renchunlin.taobaounion.utils.ToastUtil;
import com.renchunlin.taobaounion.view.ITicketPagerCallback;

import butterknife.BindView;

/*
 * class title:
 * Author by RenChunLin, Email 18957806320@163.com, Date on 2021/1/19.
 * PS: Not easy to write code, please indicate.
 */
public class TicketActivity extends BaseActivity implements ITicketPagerCallback {

    private ITicketPresenter mTicketPresenter;
    private boolean mHasTaobaoApp = false;

    @BindView(R.id.ticket_cover)
    public ImageView mCover;

    @BindView(R.id.ticket_code)
    public EditText mTicketCode;

    @BindView(R.id.ticket_copy_or_open_btn)
    public TextView mOpenOrCopyBtn;

    @BindView(R.id.iv_back)
    public ImageView back;

    @BindView(R.id.ticket_cover_loading)
    public LoadingView loadingView;

    @BindView(R.id.ticket_load_retry)
    public TextView retryLoadText;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_ticket;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initEvent() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mOpenOrCopyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //复制淘口令
                //拿到内容
                String ticketCode = mTicketCode.getText().toString().trim();
                ClipboardManager cm = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                //复制到粘贴板
                ClipData clipData = ClipData.newPlainText("sob_taobao_ticket_code", ticketCode);
                cm.setPrimaryClip(clipData);
                //判断有没有淘宝
                if (mHasTaobaoApp) {
                    //如果有就打开淘宝
                    Intent taobaoIntent = new Intent();
                    ComponentName componentName = new ComponentName("com.taobao.taobao", "com.taobao.tao.TBMainActivity");
                    taobaoIntent.setComponent(componentName);
                    startActivity(taobaoIntent);
                } else {
                    //没有提示复制成功
                    ToastUtil.showToast("已复制，粘贴分享，或打开淘宝");
                }
            }
        });
    }

    @Override
    protected void initPresenter() {
        mTicketPresenter = PresenterManager.getInstance().getTicketPresenter();
        if (mTicketPresenter != null) {
            mTicketPresenter.registerViewCallback(this);
        }
        //判断是否有安装淘宝
        // logcat | grep taobao
        // act=android.intent.action.VIEW
        // dat=http://m.taobao.com/...
        // flg=0x4000000
        // pkg=com.taobao.taobao
        // cmp=com.taobao.taobao/com.taobao.tao.TBMainActivity
        // 包名是：com.taobao.taobao
        // 检查是否安装淘宝应用
        PackageManager packageManager = getPackageManager();
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo("com.taobao.taobao", PackageManager.MATCH_UNINSTALLED_PACKAGES);
            mHasTaobaoApp = packageInfo != null;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            mHasTaobaoApp = false;
        }
        //根据这个值去修改ui
        mOpenOrCopyBtn.setText(mHasTaobaoApp ? "打开淘宝领券" : "复制淘口令");
    }

    @Override
    protected void release() {
        if (mTicketPresenter != null) {
            mTicketPresenter.unregisterViewCallback(this);
        }
    }

    @Override
    public void onTicketLoaded(String cover, TicketResult result) {
        //设置图片封面
        if (!TextUtils.isEmpty(cover) && mCover != null) {
            Glide.with(this).load(cover).into(mCover);
        }

        if (TextUtils.isEmpty(cover)) {
            mCover.setImageResource(R.mipmap.ic_launcher);
        }

        //设置一下code
        if (result != null && result.getData().getTbk_tpwd_create_response()
                != null && mTicketCode != null) {
            mTicketCode.setText(result.getData().getTbk_tpwd_create_response().getData().getModel());
        }
        if (loadingView != null) {
            loadingView.setVisibility(View.GONE);
        }
    }

    @Override
    public void onError() {
        if (loadingView != null) {
            loadingView.setVisibility(View.GONE);
        }

        if (retryLoadText != null) {
            retryLoadText.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onLoading() {
        if (retryLoadText != null) {
            retryLoadText.setVisibility(View.GONE);
        }
        if (loadingView != null) {
            loadingView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onEmpty() {
        if (loadingView != null) {
            loadingView.setVisibility(View.GONE);
        }
    }
}
